/*
 * Copyright (c) 2016, Freescale Semiconductor, Inc.
 * Copyright 2016-2017, 2020 NXP
 * All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include "fsl_debug_console.h"
#include "pin_mux.h"
#include "board.h"
#include "fsl_pint.h"
#include "fsl_ctimer.h"

#include "fsl_syscon.h"
#include "IR.h"
/*******************************************************************************
 * Definitions
 ******************************************************************************/
/* 中断IO */
#define DEMO_PINT_PIN_INT0_SRC kSYSCON_GpioPort0Pin7ToPintsel
#define DEMO_PINT_PIN_INT1_SRC kSYSCON_GpioPort0Pin7ToPintsel

/* 定时器 */
#define CTIMER          CTIMER0         /* Timer 0 */
#define CTIMER_MAT_OUT  kCTIMER_Match_0 /* Match output 0 */
#define CTIMER_CLK_FREQ CLOCK_GetFreq(kCLOCK_CoreSysClk)
/*******************************************************************************
 * Prototypes
 ******************************************************************************/
void ctimer_init(void);
void pint_pin(void);
void ctimer_match0_callback(uint32_t flags);
ctimer_callback_t ctimer_callback_table[] = {
    ctimer_match0_callback, NULL, NULL, NULL, NULL, NULL, NULL, NULL};

volatile uint32_t g_systickCounter;
void SysTick_Handler(void)
{
    if (g_systickCounter != 0U)
    {
        g_systickCounter--;
    }
}

void SysTick_DelayTicks(uint32_t n)
{
    g_systickCounter = n;
    while (g_systickCounter != 0U)
    {
    }
}
		
		
/* 上升沿 */
void RiseEdge_callback(pint_pin_int_t pintr, uint32_t pmatch_status)
{
	IR_FUNC.RiseEdgeIRQ();
}

/* 下降沿 */
void FallEdge_callback(pint_pin_int_t pintr, uint32_t pmatch_status)
{
	IR_FUNC.FallEdgeIRQ();
}

/* 定时器中断 */
void ctimer_match0_callback(uint32_t flags)
{
	IR_FUNC.TimeOutIRQ();
}

/*!
 * @brief Main function
 */
int main(void)
{
		uint8_t txbuf[5] = {0x12,0x34,0x56,0x78,0x9A};
    CLOCK_Select(BOARD_DEBUG_USART_CLK_ATTACH);

    BOARD_InitBootPins();
    BOARD_InitBootClocks();
    BOARD_InitDebugConsole();
		SysTick_Config(150U);
			
		PRINTF("systick: %d\r\n",SystemCoreClock);
    PRINTF("\f\r\nPINT Pin interrupt example\r\n");
		pint_pin();
		ctimer_init();

    while (1)
    {
				IR_FUNC.SendNum(txbuf, 2);
				SysTick_DelayTicks(4000);
			
				IR_FUNC.ShowNum();	
    }
}

void ctimer_init(void)
{
		ctimer_config_t config;
		ctimer_match_config_t matchConfig0;
		CTIMER_GetDefaultConfig(&config);
    CTIMER_Init(CTIMER, &config);
	
		matchConfig0.enableCounterReset = false;
    matchConfig0.enableCounterStop  = false;
    matchConfig0.matchValue         = CTIMER_CLK_FREQ/25;
    matchConfig0.outControl         = kCTIMER_Output_NoAction;
    matchConfig0.outPinInitState    = false;
    matchConfig0.enableInterrupt    = true;
		CTIMER_RegisterCallBack(CTIMER, &ctimer_callback_table[0], kCTIMER_MultipleCallback);
		CTIMER_SetupMatch(CTIMER, kCTIMER_Match_0, &matchConfig0);
}

void pint_pin(void)
{
		SYSCON_AttachSignal(SYSCON, kPINT_PinInt0, DEMO_PINT_PIN_INT0_SRC);
    SYSCON_AttachSignal(SYSCON, kPINT_PinInt1, DEMO_PINT_PIN_INT1_SRC);
	
		PINT_Init(PINT);

    PINT_PinInterruptConfig(PINT, kPINT_PinInt0, kPINT_PinIntEnableRiseEdge, RiseEdge_callback);
    PINT_EnableCallbackByIndex(PINT, kPINT_PinInt0);

    PINT_PinInterruptConfig(PINT, kPINT_PinInt1, kPINT_PinIntEnableFallEdge, FallEdge_callback);
    PINT_EnableCallbackByIndex(PINT, kPINT_PinInt1);
}

